package eerbaar.v2.pattern.client.internal.ui;

import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;

import eerbaar.game.IGame;
import eerbaar.memory.IMemory;
import eerbaar.message.lib.LevelChangeMsg;
import eerbaar.message.lib.NotifyCommandCreatedMsg;
import eerbaar.persona.IPersona;
import eerbaar.shared.message.IMessageReader;
import eerbaar.shared.message.Message;
import eerbaar.shared.message.MsgHelper;
import eerbaar.shared.tree.INode;
import eerbaar.shared.tree.NodeCmdFactory;
import eerbaar.shared.tree.NodeFactory;
import eerbaar.v2.app.GameHelper;
import eerbaar.v2.app.core.GamePanel;
import eerbaar.v2.app.core.IGamePanelListener;

public class ProgramPanel extends GamePanel implements IValuePanelListener {

    private final Map<INode, ProgramValuePanel> map = new HashMap<INode, ProgramValuePanel>();

    public ProgramPanel(IGamePanelListener listener) {
        super(listener);
        register();
        super.setPreferredSize(new Dimension(200, 300));
    }

    private void displayCommands() {

        IGame game = GameHelper.getGame();
        if (game == null) {
            return;
        }

        super.removeAll();

        IPersona pet = game.getPersona("pet");
        IMemory memory = pet.getHead().getBrain().getMemory();
        INode cmdNode = memory.getCommandsNode();
        INode priorityNode = memory.getIdeaPriorityNode();
        INode patternNode = memory.getIdeaPatternNode();

        int y = 0;
        INode[] children = cmdNode.getChildren();
        for (INode child : children) {

            String name = child.getPayload().getKey();
            INode pNode = NodeCmdFactory.findChild(priorityNode, name);
            if (pNode == null) {
                pNode = NodeFactory.create(name);
                pNode.getAttributes().set(Integer.class, "pri", 5);
                NodeCmdFactory.add(priorityNode, pNode);
            }
            INode patterPriNode = NodeCmdFactory.findChild(patternNode, name);
            if (patterPriNode == null) {
                patterPriNode = NodeFactory.create(name);
                if (name.equals("run-from-bug")
                        || name.equals("hide-in-a-house")) {
                    patterPriNode.getAttributes().set(Integer.class, "pri", 1);
                } else if (name.equals("run-in-pack")) {
                    patterPriNode.getAttributes().set(Integer.class, "pri", 3);
                } else {
                    patterPriNode.getAttributes().set(Integer.class, "pri", 0);
                }
                NodeCmdFactory.add(patternNode, patterPriNode);
            }

            ProgramValuePanel panel = null;
            panel = this.map.get(pNode);
            if (panel == null) {
                panel = new ProgramValuePanel(name, pNode, patterPriNode, this);
                this.map.put(pNode, panel);
            }

            Dimension size = panel.getPreferredSize();
            int w = size.width;
            int h = size.height;
            panel.setBounds(0, y, w, h);
            y += h;
            super.add(panel);
        }

        super.getPanel().setVisible(false);
        super.getPanel().setVisible(true);
    }

    @Override
    public void onSizeChanged() {
        this.displayCommands();
    }

    private void register() {

        IMessageReader reader = new IMessageReader() {

            @Override
            public boolean read(Message msg) {

                displayCommands();

                return false;
            }
        };
        MsgHelper.registerReader(NotifyCommandCreatedMsg.class, reader);
        MsgHelper.registerReader(LevelChangeMsg.class, reader);
    }

}
